package com.feeyin.radar.msg.controller;

import com.feeyin.radar.commons.DatePropertyEditor;
import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.msg.model.Comment;
import com.feeyin.radar.msg.model.Message;
import com.feeyin.radar.msg.service.EmployeeBBSService;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.RedirectView;

public class EmployeeBBSController extends MultiActionController
{
  private EmployeeBBSService employeeBBSService;
  private String employeeBBS;
  private String addEmployeeBBS;
  private String viewEmployeeBBS;

  public String getEmployeeBBS()
  {
    return this.employeeBBS;
  }

  public void setEmployeeBBS(String employeeBBS) {
    this.employeeBBS = employeeBBS;
  }

  public EmployeeBBSService getEmployeeBBSService() {
    return this.employeeBBSService;
  }

  public void setEmployeeBBSService(EmployeeBBSService employeeBBSService) {
    this.employeeBBSService = employeeBBSService;
  }

  public String getAddEmployeeBBS() {
    return this.addEmployeeBBS;
  }

  public void setAddEmployeeBBS(String addEmployeeBBS) {
    this.addEmployeeBBS = addEmployeeBBS;
  }

  public String getViewEmployeeBBS() {
    return this.viewEmployeeBBS;
  }

  public void setViewEmployeeBBS(String viewEmployeeBBS) {
    this.viewEmployeeBBS = viewEmployeeBBS;
  }

  public ModelAndView display(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    model.put("result", request.getParameter("result"));
    Integer userId = (Integer)request.getSession().getAttribute("userId");
    String sign = (String)request.getSession().getAttribute("isManageEmployeeBBS");

    String sFlag = request.getParameter("sFlag");
    String subject = "";
    if ((!StringUtils.isEmpty(sFlag)) && ("1".equals(sFlag))) {
      subject = request.getParameter("subject");
    }
    model.put("sFlag", sFlag);
    model.put("subject", subject);

    Page page = new Page();

    page.getPageList(this.employeeBBSService.getPageEmployeeBBSsCount(subject).intValue(), null, request);
    model.put("employeeBBSs", this.employeeBBSService.getPageEmployeeBBSs(page, subject));

    return new ModelAndView(getEmployeeBBS(), model);
  }

  public ModelAndView preAddEmployeeBBS(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    model.put("result", request.getParameter("result"));
    return new ModelAndView(getAddEmployeeBBS(), model);
  }

  public ModelAndView addEmployeeBBS(HttpServletRequest request, HttpServletResponse response, Message message)
  {
    boolean result = false;
    int success = 0;
    if (message != null) {
      result = this.employeeBBSService.insertEmployeeBBS(message);
      if (result) success = 1;
    }
    return new ModelAndView(new RedirectView("./employeeBBS.do?action=preAddEmployeeBBS&result=" + success));
  }

  public ModelAndView addEmployeeBBSComment(HttpServletRequest request, HttpServletResponse response, Comment comment)
  {
    boolean result = false;
    int success = 0;
    if (comment != null) {
      result = this.employeeBBSService.insertEmployeeBBSComment(comment);
      if (result) success = 1;
    }
    return new ModelAndView(new RedirectView("./employeeBBS.do?action=viewEmployeeBBS&result=" + success + "&id=" + comment.getMessageId() + "&cSign=1"));
  }

  public ModelAndView deleteEmployeeBBS(HttpServletRequest request, HttpServletResponse response)
  {
    boolean result = false;
    int success = 0;
    String[] ids = request.getParameterValues("id");
    if ((ids != null) && (ids.length > 0)) {
      result = this.employeeBBSService.deleteEmployeeBBS(ids);
      if (result) success = 1;
    }
    return new ModelAndView(new RedirectView("./employeeBBS.do?action=display&result=" + success));
  }

  public ModelAndView deleteEmployeeBBSComment(HttpServletRequest request, HttpServletResponse response)
  {
    boolean result = false;
    int success = 0;
    String[] ids = request.getParameterValues("id");
    String messageId = request.getParameter("messageId");
    if ((ids != null) && (ids.length > 0)) {
      result = this.employeeBBSService.deleteEmployeeBBSComment(ids);
      if (result) success = 2;
    }
    return new ModelAndView(new RedirectView("./employeeBBS.do?action=viewEmployeeBBS&result=" + success + "&id=" + messageId + "&cSign=1"));
  }

  public ModelAndView viewEmployeeBBS(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    model.put("result", request.getParameter("result"));
    String cSign = request.getParameter("cSign");
    String id = request.getParameter("id");
    if (!StringUtils.isEmpty(id)) {
      Message employeeBBS = new Message();
      employeeBBS = this.employeeBBSService.getEmployeeBBSById(Integer.valueOf(Integer.parseInt(id)));
      model.put("employeeBBS", employeeBBS);

      if ((employeeBBS != null) && ((StringUtils.isEmpty(cSign)) || (!"1".equals(cSign)))) {
        Integer viewNum = employeeBBS.getViewNum();
        if (viewNum != null)
          viewNum = Integer.valueOf(viewNum.intValue() + 1);
        else {
          viewNum = Integer.valueOf(1);
        }
        this.employeeBBSService.updateEmployeeBBSViewNum(employeeBBS.getId(), viewNum);
      }
    }
    return new ModelAndView(getViewEmployeeBBS(), model);
  }

  protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception
  {
    binder.registerCustomEditor(Date.class, new DatePropertyEditor());
  }
}